<template>
  <el-dialog :close-on-click-modal='false' v-drag append-to-body :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" top="10vh" width="550px" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="120px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('用户名')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" style="width:215px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('所属用户组')" prop="fgroup">
          <el-select size="small" ref="fgroup" multiple v-model="record.fgroup" @remove-tag='removeTagClick'>
            <el-option v-for="item in fgroupList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('IP类型')" prop="itype">
          <el-radio v-model="record.itype" class="ipv4" label="ipv4">IPv4</el-radio>
          <el-radio v-model="record.itype" class="ipv6" label="ipv6">IPv6</el-radio>
        </el-form-item>
        <el-form-item :label="$t('状态')" prop="status">
          <el-radio v-model="record.status" class="able" label="able">{{$t('启用')}}</el-radio>
          <el-radio v-model="record.status" class="disable" label="disable">{{$t('禁用')}}</el-radio>
        </el-form-item>
        <el-form-item :label="$t('认证方式')" prop="atype">
          <el-radio v-model="record.atype" class="connect" label="no-connect">{{$t('暂不关联')}}</el-radio>
          <el-radio v-model="record.atype" class="local" label="local-auth">{{$t('本地认证')}}</el-radio>
          <el-radio v-model="record.atype" class="third" label="third-auth">{{$t('第三方认证')}}</el-radio>
        </el-form-item>
        <el-form-item :label="$t('认证服务器名')" prop="aserver" v-if="record.atype === 'third-auth' || record.atype === 'local-auth'">
          <el-select size="small" ref="aserver" multiple v-model="record.aserver">
            <el-option v-for="item in aserverList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('本地认证密码')" prop="lpassword" v-if="record.atype === 'local-auth'">
          <el-input v-emoji v-model="record.lpassword" ref="lpassword" style="width:300px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('描述')" prop="desc">
          <el-input v-emoji v-model="record.desc" style="width:300px;" ref="desc" size='small' :autosize="{ minRows: 4 }" type="textarea" @keydown.native="handleKeyCode($event)"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getUserArray, addUser } from '@/api/user/user'
import { getServerArray } from '@/api/user/server'
import { inputClass, texttextClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'UserDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        fgroup: ['root'],
        itype: 'ipv4',
        status: 'able',
        atype: 'no-connect',
        aserver: '',
        lpassword: '',
        desc: ''
      },
      recordCopy: {
        name: '',
        fgroup: ['root'],
        itype: 'ipv4',
        status: 'able',
        atype: 'no-connect',
        aserver: '',
        lpassword: '',
        desc: ''
      },
      fgroupList: [],
      aserverList: [],
      nowName: null,
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        fgroup: [{ required: false, validator: validateName, trigger: 'blur' }],
        lpassword: [{ required: false, validator: validateName, trigger: 'blur' }],
        aserver: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.desc': {
      handler (newVal) {
        this.record.desc = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    },
    'record.atype' (val) {
      if (val === 'third-auth' || val === 'local-auth') {
        this.$nextTick(() => {
          const selectData = ['aserver']
          selectClass(this.$refs, selectData)
        })
      }
      if (val === 'local-auth') {
        this.$nextTick(() => {
          const data = ['lpassword']
          inputClass(this.$refs, data)
        })
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName']
        const textdata = ['desc']
        const selectData = ['fgroup']
        selectClass(this.$refs, selectData)
        texttextClass(this.$refs, textdata)
        inputClass(this.$refs, data)
      })
      getUserArray().then(res => {
        if (res.status === 0) {
          this.fgroupList = res.info
        }
      })
      getServerArray().then(res => {
        if (res.status === 0) {
          this.aserverList = res.info
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addUser(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.$emit('userListData', this.record.name)
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.fgroupList = []
      this.aserverList = []
      this.nowName = null
      this.record = JSON.parse(JSON.stringify(this.recordCopy))
      this.$refs.form.resetFields()
    },
    removeTagClick (item) {
      if (item === 'root') {
        this.record.fgroup = ['root'].concat(this.record.fgroup)
      }
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
